//
//  SCAllCountryViewController.m
//  LastOne
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 sc. All rights reserved.
//

#import "SCAllCountryViewController.h"
#import "SCImageCollectionViewCell.h"
#import "SCCityViewController.h"
#import "HTTPMethod.h"
#import <UIImageView+WebCache.h>
#import "GiFHUD.h"
#import "NightHeader.h"
#import "FitHeader.h"
#define urlString @"http://open.qyer.com/place/city/get_city_list?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=861138023945773&track_app_version=6.7&track_app_channel=360m&track_device_info=mx3&track_os=Android4.2.1&app_installtime=1443017336409&lat=38.883593&lon=121.54466&page=%ld&countryid=%@"

@interface SCAllCountryViewController ()<UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource>
@property(nonatomic, assign)NSInteger page;
@property(nonatomic, retain)NSMutableArray *dataSource;
@property(nonatomic, retain)UICollectionView *collectionView;
@end

@implementation SCAllCountryViewController

static NSString * const reuseIdentifier = @"Cell";

//-(void)netError:(NSNotification *)notification
//{
//    //加载时停止加载
//    [self.collectionView.footer endRefreshing];
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    //加载页面
    [GiFHUD setGifWithImageName:@"loading.gif"];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netError:) name:@"net" object:nil];
    
    self.dataSource = [NSMutableArray array];
    _page = 1;
    [self getData];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.sectionInset = UIEdgeInsetsMake(15 * FitHeight, 15 * FitWidth, 15 * FitHeight, 15 * FitWidth);
    flowLayout.minimumInteritemSpacing = 15 * FitWidth;
    flowLayout.minimumLineSpacing = 15 * FitHeight;
    flowLayout.itemSize = CGSizeMake((self.view.frame.size.width - 15 * FitWidth * 3 - 5 * FitWidth) / 2.0, 200 * FitHeight);
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 - 49) collectionViewLayout:flowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView NightWithType:UIViewColorType2];
    [self.view addSubview:_collectionView];
    [self.collectionView registerClass:[SCImageCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
//    self.collectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        _page++;
//        [self initData];
//    }];
}

- (void)getData
{
    [GiFHUD show];
    [HTTPMethod getDataByString:[NSString stringWithFormat:urlString, _page, _cityId] BodyDic:nil WithDataBlock:^(id data) {
        [_dataSource addObjectsFromArray:[data objectForKey:@"data"]];
        [self.collectionView reloadData];
       // [self.collectionView.footer endRefreshing];
        [GiFHUD dismiss];
    }];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    if (_dataSource.count == 0) {
        return 0;
    }
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SCImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:[[_dataSource objectAtIndex:indexPath.row] objectForKey:@"photo"]]placeholderImage:[UIImage imageNamed:@"placeholder"]];
    cell.imageView.backgroundColor = [UIColor grayColor];
    cell.nameString =  [[_dataSource objectAtIndex:indexPath.row] objectForKey:@"catename"];
    cell.ennameString = [[_dataSource objectAtIndex:indexPath.row] objectForKey:@"catename_en"];
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SCCityViewController *cityVC = [[SCCityViewController alloc] init];
    cityVC.cityId = [[_dataSource objectAtIndex:indexPath.row] objectForKey:@"id"];
    [self.navigationController pushViewController:cityVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
